/**
 * 17/gen/2012 - fabio.melillo@gmail.com
 */
package unisannio.fm.interactive;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import unisannio.fm.R;
import android.app.AlertDialog;
import android.app.ExpandableListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;

/**
 * @author fabio
 *
 */
public class AllQuestionsActivity extends ExpandableListActivity {
	
	private Bundle totale;
	private List elenchi;
	private List gruppi;
	public static final String filename="RelevantAnswers.txt";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		totale=this.mergeBundles(getIntent().getExtras());
		SimpleExpandableListAdapter expList=new SimpleExpandableListAdapter(this, createGroups(), R.layout.grouprow, new String[] {"GroupItem"}, new int[] {R.id.row_name}, createChildList(), R.layout.childrow, new String[] {"SubItem"}, new int[] {R.id.grp_child});
		setListAdapter(expList);
	}
	
	private Bundle mergeBundles(Bundle extras) {
		if (totale==null)
			totale=new Bundle();
		totale.putAll(extras);
		return totale;
	}

	@SuppressWarnings("unchecked")
	private List createGroups(){
		ArrayList res=new ArrayList();
		Set<String> chiavi=totale.keySet();
		for (String chiave : chiavi) {
			HashMap<String, String> map=new HashMap<String, String>();
			map.put("GroupItem",chiave);
			res.add(map);
		}
		gruppi=res;
		return (List)res;
	}
	
	@SuppressWarnings("unchecked")
	private List createChildList(){
		ArrayList res=new ArrayList();
		Set<String> chiavi=totale.keySet();
		for (String chiave : chiavi) {
			ArrayList figli=new ArrayList();
			ArrayList<String> risposte=new ArrayList<String>();
			String risposta=totale.getString(chiave);
			if (risposta==null)
				risposte=totale.getStringArrayList(chiave);
			else
				risposte.add(risposta);
			for (String curr : risposte) {
				HashMap<String, String> child=new HashMap<String, String>();
				child.put("SubItem", curr);
				figli.add(child);
			}
			res.add(figli);
		}
		elenchi=res;
		return res;
	}
	
	public boolean onChildClick( ExpandableListView parent, View v, int groupPosition,int childPosition,long id) {
		String domanda=((HashMap<String,String>) gruppi.get(groupPosition)).get("GroupItem");
		String selezionato=((HashMap<String,String>) ((List) elenchi.get(groupPosition)).get(childPosition)).get("SubItem");
//		Toast toast=Toast.makeText(this,domanda+"\n"+selezionato, Toast.LENGTH_LONG);
//		toast.show();
		
		//da inserire una dialog per i salvataggi
		
			LayoutInflater inflater=LayoutInflater.from(this);
			View addView = inflater.inflate(R.layout.response, null);
			final QuestionDialogWrapper wrapper = new QuestionDialogWrapper(addView, domanda, selezionato);
			new AlertDialog.Builder(this)
				.setTitle(domanda)
				.setView(addView)
				.setPositiveButton("SAVE", new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int which) {
						//su SEND invia i vari pacchetti alle varie actions registrate
						processSave(wrapper);
					}
				})
				.show();
		
    	return true;
    }
	
	
	protected void processSave(QuestionDialogWrapper wrapper) {
		//salvataggio su file
		File file=new File(getExternalFilesDir(null), filename);
		try {
			PrintStream ps=new PrintStream(new FileOutputStream(file, true));
			String quest=wrapper.getDomanda();
			quest.replace("\n", " ");
			ps.println("###"+quest+"\n"+wrapper.getRisposta());
			ps.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		
	}


	class QuestionDialogWrapper {
		TextView questionField=null;
		View base=null;
		String domanda,risposta;
		
		public String getDomanda() {
			return domanda;
		}

		public void setDomanda(String domanda) {
			this.domanda = domanda;
		}

		public String getRisposta() {
			return risposta;
		}

		public void setRisposta(String risposta) {
			this.risposta = risposta;
		}

		QuestionDialogWrapper(View base, String domanda, String risposta){
			this.base=base;
			questionField=(TextView)base.findViewById(R.id.responseText);
			this.domanda=domanda;
			this.risposta=risposta;
			questionField.setText(risposta);
		}
		
		
	}
}
